const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            Land: [],
            Crop: [],
            childDataForm:[{
                Interplant:'0',
            }]
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.dataForm.Type = getQueryObject(ls_URL, "type")?getQueryObject(ls_URL, "type"):'0';// 获取 nodeKey 参数
        this.getCategory();
    },
    methods: {
        configCategory(arr1,arr2,str1,str2){
            arr1.forEach(item=>{
                if (!item.Crop_Category||!item.Crop_Category_SerialNumber) {
                    item.Crop_Category = item[str1];
                    item.Crop_Category_SerialNumber = item[str2];
                }
                if (item.children && item.children.length > 0) {
                    item.children = this.configCategory(item.children,arr2,str1,str2);
                } else {
                    item.children = [];
                    arr2.filter(i=>i.Crop_Category_SerialNumber == item.Crop_Category_SerialNumber).forEach(obj=>{
                        item.children.push({
                            Crop_Category:obj.Crop_Name,
                            Crop_Category_SerialNumber:obj.Crop_SerialNumber
                        })
                    })
                }
                if (item.children.length==0) {
                    arr1 = arr1.filter(i=>i.Crop_Category_SerialNumber != item.Crop_Category_SerialNumber);
                }
            });
            return arr1;
        },
        changeSwitch(val,item){
            if (val == '0') {
                this.childDataForm.forEach(row => {
                    row.Interplant = '1'
                })
                item.Interplant = '0'
            }
        },
        handleLand(val) { //承包人
            // console.log(val);
            let obj = this.Land.filter(i=>i.Natural_Resource_SerialNumber == val)[0];
            if (obj) {
                this.dataForm.Contractor_Name = obj.Contractor_Name;
                // console.log(obj);
            }
        },
        getCategory() {
            let str = '';
            if (this.dataForm.Type=='0') {
                str = `AND (natural_resources_management.Natural_Resource_Usage_SerialNumber LIKE '01%'
                OR  natural_resources_management.Natural_Resource_Usage_SerialNumber = '1201'
                OR  natural_resources_management.Natural_Resource_Usage_SerialNumber = '1202'
                OR  natural_resources_management.Natural_Resource_Usage_SerialNumber = '1203')`
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                    type: "GET",
                    data: {
                        TableName:'crop_category',
                        NodeCode:''
                    },
                    success: (data1) => {               
                        data1 = data1.filter(i=>i.children && i.children.length > 0);
                        $.ajax({
                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/generalQueryByTableName`,
                            type: "POST",
                            data: {
                                Data: 'crop',
                                Condition: "Row_Status='0'",
                            },
                            success: (data) => {
                                this.Crop = this.configCategory(data1,data,'Crop_Category','Crop_Category_SerialNumber');
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest);
                            },
                            dataType: "json",
                        });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                    dataType: "json",
                });
                
            } else if (this.dataForm.Type=='1') {
                str = "AND (natural_resources_management.Natural_Resource_Usage_SerialNumber LIKE '04%' OR natural_resources_management.Natural_Resource_Usage_SerialNumber = '1202')"
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                    type: "GET",
                    data: {
                        TableName:'poultry_category',
                        NodeCode:''
                    },
                    success: (data1) => {               
                        data1 = data1.filter(i=>i.children && i.children.length > 0);
                        $.ajax({
                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/generalQueryByTableName`,
                            type: "POST",
                            data: {
                                Data: 'poultry',
                                Condition: "Row_Status='0'",
                            },
                            success: (data) => {
                                data.forEach(item=>{
                                    item.Crop_Name = item.Poultry_Name;
                                    item.Crop_SerialNumber = item.Poultry_SerialNumber;
                                    item.Crop_Category_SerialNumber = item.Poultry_Category_SerialNumber;
                                })
                                this.Crop = this.configCategory(data1,data,'Poultry_Category','Poultry_Category_SerialNumber');
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest);
                            },
                            dataType: "json",
                        });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                    dataType: "json",
                });
            } else if (this.dataForm.Type=='2') {
                str = "AND natural_resources_management.Natural_Resource_Usage_SerialNumber LIKE '11%'"
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                    type: "GET",
                    data: {
                        TableName:'offspring_seed_category',
                        NodeCode:''
                    },
                    success: (data1) => {               
                        data1 = data1.filter(i=>i.children && i.children.length > 0);
                        $.ajax({
                            headers: requestHeaders(),
                            url: `${mh}/generalBusiness/generalQueryByTableName`,
                            type: "POST",
                            data: {
                                Data: 'offspring_seed',
                                Condition: "Row_Status='0'",
                            },
                            success: (data) => {
                                data.forEach(item=>{
                                    item.Crop_Name = item.Offspring_Seed_Name;
                                    item.Crop_SerialNumber = item.Offspring_Seed_SerialNumber;
                                    item.Crop_Category_SerialNumber = item.Offspring_Seed_Category_SerialNumber;
                                })
                                this.Crop = this.configCategory(data1,data,'Offspring_Seed_Category','Offspring_Seed_Category_SerialNumber');
                            },
                            complete: (XMLHttpRequest) => {
                                requestPretreatment(XMLHttpRequest);
                            },
                            dataType: "json",
                        });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest)
                    },
                    dataType: "json",
                });   
            }
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 't_natural_resources_registration',
                    Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%"))),
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent(str))),
                },
                success: (data) => {
                    this.Land = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let land_crop_record = []
                this.childDataForm.forEach(item=>{
                    land_crop_record.push({...this.dataForm , ...item});
                })
                let lJSONObject_DataSet = {
                    Add:{
                        land_crop_record
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSaveByRequestBody",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.history.go(-1);
                    }
                    this.loading = false;
                })
                // $.ajax({
                //     headers: requestHeaders("JSON"),
                //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                //     method: "post",
                //     data: JSON.stringify({
                //         Data: lJSONObject_DataSet,
                //     }),
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             window.history.go(-1);
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         this.loading = false;
                //         requestPretreatment(XMLHttpRequest)
                //     }
                // });
            } else {
                this.$message({
                    message: '存在未填写的必填项！',
                    type: 'warning'
                });
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-cascader', ylCascader);
app.component('yl-select',ylSelect)
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
